package com.demo.web;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("/file")
public class FileUploadController {
    @RequestMapping("/upload")
    public String upload(@RequestParam(name="file") MultipartFile multipartFile){
        // 获取文件的真实名
        String fileName = multipartFile.getOriginalFilename();
        // 获取上传的文件大小
        long size = multipartFile.getSize();
        // 获取上传的文件的格式
        String contentType = multipartFile.getContentType();
        System.out.println("fileName = " + fileName);
        System.out.println("size = " + size);
        System.out.println("contentType = " + contentType);
        // 需要考虑将文件存储到什么位置（本地磁盘、阿里云、腾讯云）
        try {
            // 将文件存储在本地磁盘中（用于演示），真实中需要存储到真实文件服务器中
            // multipartFile.transferTo(new File("D:\\" + fileName));
            return aliyunOSS(multipartFile);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "上传成功";
    }
    public String aliyunOSS(MultipartFile multipartFile){
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String bucketName = "tianjin-java2";
        String region = "cn-beijing";
        //  AccessKey ID   LTAI5tQQYTNT48o5JQtNwmoC
        //  AccessKey Secret mHgiViLkv5pnNnbCCGPaa3jVugOlIB
        String accessKeyId = "LTAI5tQQYTNT48o5JQtNwmoC";
        String accessKeySecret = "mHgiViLkv5pnNnbCCGPaa3jVugOlIB";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请先配置环境变量
        DefaultCredentialProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        try {
            // 1. 创建存储空间（Bucket）
            // ossClient.createBucket(bucketName);
            // System.out.println("1. Bucket " + bucketName + " 创建成功。");
            // 2. 上传文件
            String objectName = multipartFile.getOriginalFilename();
            ossClient.putObject(bucketName, objectName, multipartFile.getInputStream());
            System.out.println("2. 文件 " + objectName + " 上传成功。");
            // 将给oss上存储的文件路径信息，给客户端返回
            // tianjin-java2.oss-cn-beijing.aliyuncs.com
            //
            https://tianjin-java2.oss-cn-beijing.aliyuncs.com/01-%E7%AE%A1%E7%90%86%E5%91%98%E7%99%BB%E5%BD%95.png
            return "https://" + bucketName + ".oss-cn-beijing.aliyuncs.com/" + objectName;

        }catch (Exception ce) {
            ce.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
